package xctx

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"ns/pkg/consts"
	"ns/pkg/xcrypt"
)

func GetUidFromCtx(ctx context.Context) int64 {
	var uid int64
	if jsonUid, ok := ctx.Value(consts.UserIDKey).(json.Number); ok {
		if int64Uid, err := jsonUid.Int64(); err == nil {
			uid = int64Uid
		} else {
			logx.WithContext(ctx).Errorf("GetUidFromCtx err : %+v", err)
		}
	}
	return uid
}

func GetMobileFromCtx(ctx context.Context, key string) string {
	var mobile string
	ctxMobile := ctx.Value("mobile")
	mobile = fmt.Sprintf("%v", ctxMobile)
	decryptMobile, err := xcrypt.DecryptAESWithECB(mobile, key)
	if err != nil {
		logx.WithContext(ctx).Errorf("GetMobileFromCtx err : %+v", err)
	} else {
		mobile = decryptMobile
	}

	return mobile
}
